package util;

import java.util.List;
import java.util.Map;

public class $ {
	
	public static String EMPTY_REQUIRED_PARAMETER = "EMPTY_REQUIRED_PARAMETER";
	
	public static int toInt(String s, int i){
    	try{
    		return Integer.parseInt(s);
    	}catch(Exception e){
    		return i;
    	}
    }
	public static int toInt(String s){
    	try{
    		return Integer.parseInt(s);
    	}catch(Exception e){
    		return 0;
    	}
    }
	
	public static boolean isEmpty(String s){
		if(s==null || s.length()==0) return true;
		return false;
	}

	public static String toJSON(Map<String,String> map){
		String json = "{";
		for(String key : map.keySet()){
			json += "\""+escape(key)+"\": \""+escape(map.get(key))+"\",";
		}
		if(json.endsWith(",")) json = json.substring(0, json.length()-1);
		json += "}";
		return json;
	}
	public static String toJSON(List<Map<String,String>> list){
		String json = "[";
		for(Map<String,String> map : list){
			json += toJSON(map)+",";
		}
		if(json.endsWith(",")) json = json.substring(0, json.length()-1);
		json += "]";
		return json;
	}
	public static String toJSON(String name, List<Map<String,String>> list){
		String json = "{\""+escape(name)+"\":";
		json += toJSON(list); 
		json += "}";
		return json;
	}
	
	public static String toJSON(String key, String value){
		return "{\""+escape(key)+"\":\""+escape(value)+"\"}";
	}
	
	public static String escape(String string){
		return string == null ? "" : string.replace("\"", "\\\"");
	}
}
